home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / RCShaders / RCBlueMarble.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  2.9 KB  |  80 lines

  1. /* Listing 16.19  Blue marble surface shader*/
  2. /*
  3.  * blue_marble(): a marble stone texture in shades of blue
  4.  */
  5.  
  6. surface
  7. RCBlueMarble(
  8.           float   Ks    = .4, 
  9.                   Kd    = .6, 
  10.                   Ka    = .1,
  11.                   roughness = .1,
  12.                   txtscale = 1;
  13.           color   specularcolor = 1)
  14. {
  15.     point PP;            /* scaled point in shader space */
  16.     float csp;           /* color spline parameter */
  17.     point Nf;            /* forward-facing normal */
  18.     point V;             /* for specular() */
  19.     float pixelsize, twice, scale, weight, turbulence;
  20.  
  21.     /* Obtain a forward-facing normal for lighting calculations. */
  22.     Nf = faceforward( normalize(N), I);
  23.     V = normalize(-I);
  24.  
  25.     /*
  26.      * Compute "turbulence" a la [PERLIN85]. Turbulence is a sum of 
  27.      * "noise" components with a "fractal" 1/f power spectrum. It gives the
  28.      * visual impression of turbulent fluid flow (for example, as in the 
  29.      * formation of blue_marble from molten color splines!). Use the 
  30.      * surface element area in texture space to control the number of 
  31.      * noise components so that the frequency content is appropriate 
  32.      * to the scale. This prevents aliasing of the texture.
  33.      */
  34.     PP = transform("shader", P) * txtscale;
  35.     pixelsize = sqrt(area(PP));
  36.     twice = 2 * pixelsize;
  37.     turbulence = 0;
  38.     for (scale = 1; scale > twice; scale /= 2) 
  39.         turbulence += scale * noise(PP/scale);
  40.  
  41.     /* Gradual fade out of highest-frequency component near limit */
  42.     if (scale > pixelsize) {
  43.         weight = (scale / pixelsize) - 1;
  44.         weight = clamp(weight, 0, 1);
  45.         turbulence += weight * scale * noise(PP/scale);
  46.     }
  47.  
  48.     /*
  49.      * Magnify the upper part of the turbulence range 0.75:1
  50.      * to fill the range 0:1 and use it as the parameter of
  51.      * a color spline through various shades of blue.
  52.      */
  53.     csp = clamp(4 * turbulence - 3, 0, 1);
  54.     Ci = color spline(csp,
  55.     color (0.25, 0.25, 0.35),      /* pale blue        */
  56.         color (0.25, 0.25, 0.35),  /* pale blue        */
  57.         color (0.20, 0.20, 0.30),  /* medium blue      */
  58.         color (0.20, 0.20, 0.30),  /* medium blue      */
  59.         color (0.20, 0.20, 0.30),  /* medium blue      */
  60.         color (0.25, 0.25, 0.35),  /* pale blue        */
  61.         color (0.25, 0.25, 0.35),  /* pale blue        */
  62.         color (0.15, 0.15, 0.26),  /* medium dark blue */
  63.         color (0.15, 0.15, 0.26),  /* medium dark blue */
  64.         color (0.10, 0.10, 0.20),  /* dark blue        */
  65.         color (0.10, 0.10, 0.20),  /* dark blue        */
  66.         color (0.25, 0.25, 0.35),  /* pale blue        */
  67.         color (0.10, 0.10, 0.20)   /* dark blue        */
  68.         );
  69.  
  70.     /* Multiply this color by the diffusely reflected light. */
  71.     Ci *= Ka*ambient() + Kd*diffuse(Nf);
  72.  
  73.     /* Adjust for opacity. */
  74.     Oi = Os;
  75.     Ci = Ci * Oi;
  76.  
  77.     /* Add in specular highlights. */
  78.     Ci += specularcolor * Ks * specular(Nf,V,roughness);
  79. }
  80.